package com.ibm.cognos.imt.controller;

import com.ibm.cognos.imt.model.*;
import com.ibm.cognos.imt.service.CognosCMService;
import com.ibm.cognos.imt.service.CognosReportService;
import com.ibm.cognos.imt.service.GroovyScriptHelper;
import com.ibm.cognos.imt.service.ReportHtmlFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.List;

//import org.springframework.oxm.castor.CastorMarshaller;

/**
 * Created by eric on 7/7/2014.
 */
@Controller
@RequestMapping("/api")

public class ApiController {
    @Autowired
    CognosReportService reportService;

    @Autowired
    ReportHtmlFactory reportHtmlFactory;

    @Autowired
    CognosCMService cmService;

    @Autowired
    GroovyScriptHelper groovyScriptHelper;

//    @Autowired
//    CastorMarshaller marshaller;

    @RequestMapping(value = "/report/html", method = RequestMethod.POST, headers={"Content-type=application/json"})
    @ResponseBody
    public ReportHtml runReportForHtml(@RequestBody ReportPath reportPath) throws RemoteException, ServiceException, MalformedURLException {
        String[] result = reportService.runReport(reportPath.getReportPath(), "HTML");
//        System.out.println(result);
        return reportHtmlFactory.parseOutputPages(result).get(0);
    }

    @RequestMapping(value = "/groovySpec/html", method = RequestMethod.POST, headers={"Content-type=application/json"})
    @ResponseBody
    public ReportHtml runGroovySpecForHtml(@RequestBody ReportPath reportPath) throws RemoteException, ServiceException, MalformedURLException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String xmlSpec = groovyScriptHelper.invokeGenerateSpecMethod(reportPath.getReportPath());
        return reportHtmlFactory.parseOutputPages(reportService.runSpec(xmlSpec, "HTML")).get(0);
    }

    @RequestMapping(value = "/spec/data", method = RequestMethod.POST, headers={"Content-type=application/json"})
    public ResponseEntity<String> runSpecForData(@RequestBody ReportSpec spec) throws IOException, ServiceException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String[] data = reportService.runSpec(spec.getSpec(), "XML");
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_XML);
        responseHeaders.set("HeaderKey", "HeaderData");
        return new ResponseEntity<String>(data[0], responseHeaders,   HttpStatus.OK);
//        return marshaller.unmarshal(new StreamSource(new StringReader(data[0])));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, headers={"Content-type=application/json"})
    @ResponseBody
    public int add(@RequestBody TwoInt object) {
        return object.sum();
    }

    @RequestMapping(value = "/spec/html", method = RequestMethod.POST, headers={"Content-type=application/json"})
    @ResponseBody
    public ReportHtml runSpecForHtml(@RequestBody ReportSpec spec) throws MalformedURLException, ServiceException {
        String[] result = reportService.runSpec(spec.getSpec(), "HTML");
        return reportHtmlFactory.parseOutputPages(result).get(0);
//        return reportHtmlFactory.parse(reportService.testReportHtml());
    }

    @RequestMapping(value = "/reports", method = RequestMethod.GET)
    @ResponseBody
    public List<SavedReport> getAllReportNames() throws Exception {
        return cmService.getAllReports();
    }


    @RequestMapping(value = "/models", method = RequestMethod.GET)
    @ResponseBody
    public String getAllModels() throws Exception {
        return cmService.getModel().toJson();
    }

    @RequestMapping(value = "/report/xml/{reportPath}", method = RequestMethod.GET)
    @ResponseBody
    public String runXmlReport(@PathVariable String reportPath) {
        return reportPath;
    }




}


